<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    append-to-body
    :close-on-click-modal="false"
    @close="closeDialog()"
    :visible="visible"
    width="1000px"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="商品名称" prop="spuTitle">
        <el-input
          v-model="dataForm.spuTitle"
          placeholder="请输入商品名称"
        ></el-input>
      </el-form-item>

      <el-form-item label="轮播图" prop="bannerImages">
        <ImagesUpload v-model="dataForm.bannerImages"></ImagesUpload>
      </el-form-item>

      <el-form-item label="详情图" prop="detailImages">
        <ImagesUpload v-model="dataForm.detailImages"></ImagesUpload>
      </el-form-item>

      <el-row>
        <el-col :span="12">
          <el-form-item label="分类名称" prop="categoryId">
            <el-select
              v-model="dataForm.categoryId"
              filterable
              placeholder="请选择分类名称"
              clearable
              size="small"
              class="margin-left-xs"
              style="width:100%"
            >
              <el-option
                v-for="dict in pointSpuCategoryList"
                :key="dict.id"
                :label="dict.categoryName"
                :value="dict.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="兑换数量" prop="count">
            <el-input-number
              placeholder="请输入兑换数量"
              v-model="dataForm.count"
              controls-position="right"
              :min="1"
              style="width:100%"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="市场价格" prop="marketPrice">
            <el-input v-model="dataForm.marketPrice" placeholder="请输入市场价格" ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="所需积分" prop="point">
            <el-input v-model.number="dataForm.point" placeholder="请输入所需积分" ></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="排序" prop="sort">
            <el-input-number
              placeholder="请输入排序"
              v-model="dataForm.sort"
              controls-position="right"
              :min="0"
              style="width:100%"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="上架状态" prop="status">
            <el-radio-group v-model="dataForm.status" style="width:300px">
              <el-radio label="1" style="margin-right:10px" border>上架</el-radio>
              <el-radio label="0" style="margin-right:10px" border>下架</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="推荐列表" prop="hasRecommend">
            <el-switch v-model="dataForm.hasRecommend" active-value="1" inactive-value="0"></el-switch>
          </el-form-item>
        </el-col>
      </el-row>

      <el-form-item label="备注" prop="remark">
        <el-input type="textarea" :rows="4" v-model="dataForm.remark" placeholder="备注"></el-input>
      </el-form-item>
      
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit"
        >确定</el-button
      >
    </span>
  </el-dialog>
</template>

<script>
import { getObj, addObj, putObj } from "@/api/shop_point/spu";
import {getPointSpuCategoryList} from '@/api/shop_point/category';
import ImagesUpload from "@/components/imagesUpload";

export default {
  props:{
    visible: {
      type: Boolean,
      default: false
    },
  },
  data() {
    return {
      canSubmit: false,
      dataForm: {
        bannerImages:[],
        detailImages:[],
        status:'1',
        categoryId:'',
        hasRecommend:'1'
      },
      pointSpuCategoryList:[],
      dataRule: {
        spuTitle: [
          { required: true, message: "商品名称不能为空", trigger: "blur" },
        ],
        bannerImages: [
          { required: true, message: "轮播图不能为空", trigger: "blur" },
        ],
        detailImages: [
          { required: true, message: "详情图不能为空", trigger: "blur" },
        ],
        marketPrice: [
          { required: true, message: "市场价格不能为空", trigger: "blur" },
        ],
        point: [
          { required: true, message: "所需积分不能为空", trigger: "blur" },
        ],
        sort: [
          { required: true, message: "排序不能为空", trigger: "blur" },
        ],
        status: [
          { required: true, message: "上架状态不能为空", trigger: "blur" },
        ],
        count: [
          { required: true, message: "兑换数量不能为空", trigger: "blur" },
        ],
        categoryId: [
          { required: true, message: "分类名称不能为空", trigger: "blur" },
        ]
      },
    };
  },
  components: {
    ImagesUpload,
  },
  methods: {
    init(id) {
      this.canSubmit = true;
      getPointSpuCategoryList().then(res=>{
        this.pointSpuCategoryList = res.data.data
        if(id){
          getObj(id).then((response) => {
            let data = response.data.data;
            // data.bannerImages = data.bannerImages.split(',')
            // data.detailImages = data.detailImages.split(',')
            data.bannerImages = data.bannerImages ? JSON.parse(data.bannerImages) : []
            data.detailImages = data.detailImages ? JSON.parse(data.detailImages) : []
            // 市场价格转分
            data.marketPrice = this.formatMoneyToYuan(data.marketPrice)
            this.dataForm = data
          });
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.canSubmit = false;
          let requestData = JSON.parse(JSON.stringify(this.dataForm))
          requestData.bannerImages = requestData.bannerImages.map(p=>p.materialId).join()
          requestData.detailImages = requestData.detailImages.map(p=>p.materialId).join()
          // 市场价格转分
          requestData.marketPrice = this.formatMoneyToFen(requestData.marketPrice)
          console.log(requestData)
          if (requestData.id) {
            putObj(requestData)
              .then((data) => {
                this.$notify.success("修改成功");
                this.$emit("refreshDataList");
                this.closeDialog()
              })
              .catch(() => {
                this.canSubmit = true;
              });
          } else {
            addObj(requestData)
              .then((data) => {
                this.$notify.success("添加成功");
                this.$emit("refreshDataList");
                this.closeDialog()
              })
              .catch(() => {
                this.canSubmit = true;
              });
          }
        }
      });
    },
    //重置表单
    closeDialog() {
      this.$emit('update:visible', false)
    },

  },
};
</script>
